package com.itbour.service.stats.impl;

import java.util.List;
import java.util.Map;

import com.itbour.com.page.JspPage;
import com.itbour.com.page.Page;
import com.itbour.dao.stats.UserDocStatsDao;
import com.itbour.dto.stats.UserDocInfoDto;
import com.itbour.dto.stats.UserDocStatsDto;
import com.itbour.dto.tg.TypeInfoDto;
import com.itbour.service.stats.UserDocStatsService;

public class UserDocStatsServiceImpl implements UserDocStatsService {
	
	private  UserDocStatsDao  userDocStatsDao;

	@Override
	public Page pageQueryStats(Map<String, Object> param, Integer pageNo,
			Integer pageSize) {
		Page result = null;
		if (param.get("user_name") !=null ){ // 设计师名字不为空 模糊搜索
			String uId = userDocStatsDao.getUserIdByUserName(param);
			if (uId!=null && uId.length()>0){
				param.put("user_id", uId);
			} else {
				param.put("user_id", -1);
			}
		}
		
		// 实际起始位置
		int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
		int totalSize = userDocStatsDao.userDocStatsCnt(param);
		while(totalSize >0 && totalSize <= skipResults){
			skipResults= skipResults - pageSize;
			pageNo = pageNo -1;
		}
		// 分页检索
		param.put("pageSize", pageSize);
		param.put("skipResults", skipResults);
		List<UserDocStatsDto> data = userDocStatsDao.userDocStatsInfo(param);
		result = new JspPage(skipResults, totalSize, pageSize, data);
		return result;
	}
	
	@Override
	public boolean editShowViewCnt(Map<String, Object> parm) {
		try {
			userDocStatsDao.editItbTmplStatsInf(parm);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	public boolean batchSetShowApplyCnt(Map<String, Object> parm){
		try {
			userDocStatsDao.batchSetShowApplyCnt(parm);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	// 新建文档信息统计
	public Page pageQueryNewUserDoc(Map<String, Object> param, Integer pageNo,
			Integer pageSize) {
		Page result = null;
		// 实际起始位置
		int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
		//int totalSize = userDocStatsDao.newUserDocCnt(param);
		int totalSize = userDocStatsDao.newUserDocCnt_2(param);
		while(totalSize >0 && totalSize <= skipResults){
			skipResults= skipResults - pageSize;
			pageNo = pageNo -1;
		}
		// 分页检索
		param.put("pageSize", pageSize);
		param.put("skipResults", skipResults);
		//List<UserDocInfoDto> data = userDocStatsDao.newUserDocInfo(param);
		List<UserDocInfoDto> data = userDocStatsDao.newUserDocInfo_2(param);
		result = new JspPage(skipResults, totalSize, pageSize, data);
		return result;
	}
	// 所有文档个数
	public UserDocInfoDto getAllUserDocInfo(){
		//List<UserDocInfoDto> data = userDocStatsDao.getAllUserDocInfo();
		List<UserDocInfoDto> data = userDocStatsDao.getAllUserDocInfo_2();
		if(data!=null && data.size()>0) {
			return data.get(0);
		}
		return null;
	}
	
	
	public UserDocStatsDao getUserDocStatsDao() {
		return userDocStatsDao;
	}
	public void setUserDocStatsDao(UserDocStatsDao userDocStatsDao) {
		this.userDocStatsDao = userDocStatsDao;
	}
	@Override
	public List<TypeInfoDto> getTypeForCombo(Map<String, Object> param) {
		return userDocStatsDao.selectTypeInfo(param);
	}
}
